package sz.szsmk.citizencard.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import com.chenenyu.router.annotation.Route;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.util.JumpUtils;
import sz.szsmk.citizencard.widget.MyCaptureManager;

import static com.google.zxing.integration.android.IntentIntegrator.REQUEST_CODE;

/**
 * Created by hyt on 2019/4/18.
 */
@Route("scanQRCode")
public class ScanQRActivity extends AppCompatActivity {


    DecoratedBarcodeView dbvCustom;

    ImageView iv_back;

    private MyCaptureManager captureManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scanqrcode);

        dbvCustom = findViewById(R.id.dbv_custom);

        iv_back = findViewById(R.id.iv_back);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        //重要代码，初始化捕获
        captureManager = new MyCaptureManager(this, dbvCustom);
        captureManager.initializeFromIntent(getIntent(), null);
        captureManager.isreturnBarcodeImagePath(getIntent());

        initScanInterface();

        captureManager.decode();
        captureManager.setBundle(null);

        captureManager.decode();
        captureManager.onResume();


        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }


    private void initScanInterface() {
        captureManager.setOnScanResult(new MyCaptureManager.OnScanResult() {
            @Override
            public void onScanResult(Intent intent) {
                IntentResult intentResult = IntentIntegrator.parseActivityResult(REQUEST_CODE, Activity.RESULT_OK,intent);
                if(intentResult != null) {

                    String ScanResult = intentResult.getContents().trim();
                    Log.i("ScanResult",ScanResult);

                    Bundle bundle = new Bundle();
                    bundle.putString("url", ScanResult);
                    JumpUtils.jumptoNatvie(ScanQRActivity.this, "webView", 0, bundle);
                    finish();
                }
            }
        });
    }


    @Override
    protected void onPause() {
        super.onPause();
        captureManager.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        captureManager.decode();
        captureManager.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        captureManager.onDestroy();
    }




}